package org.dczl.system.controller.daping;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dczl.system.domain.vo.ExportDpProjectVo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dczl.common.idempotent.annotation.RepeatSubmit;
import org.dczl.common.web.core.BaseController;
import org.dczl.common.core.domain.R;
import org.dczl.common.core.validate.AddGroup;
import org.dczl.common.core.validate.EditGroup;
import org.dczl.common.excel.utils.ExcelUtil;
import org.dczl.system.domain.bo.DpProjectBo;
import org.dczl.system.service.IDpProjectService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目
 * 前端访问路由地址为:/core/project
 *
 * @author jia
 * @date 2025-04-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project")
public class DpProjectController extends BaseController {

    private final IDpProjectService dpProjectService;

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "实验室教学安排表", ExportDpProjectVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        List<ExportDpProjectVo> result = ExcelUtil.importExcel(file.getInputStream(), ExportDpProjectVo.class);
        dpProjectService.importExcel(result);
        return R.ok();
    }

    /**
     * 新增项目
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DpProjectBo bo) {
        return toAjax(dpProjectService.insertByBo(bo));
    }

    /**
     * 修改项目
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DpProjectBo bo) {
        return toAjax(dpProjectService.updateByBo(bo));
    }

    /**
     * 删除项目
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(dpProjectService.deleteWithValidByIds(List.of(ids), true));
    }
}
